package com.softa.service.system.impl;

import com.softa.dao.mapper.base.SysFileMapper;
import com.softa.dao.mapper.relation.system.SysFileRelationMapper;
import com.softa.dao.model.Product;
import com.softa.dao.model.SysFile;
import com.softa.enummodel.FileTypeEnum;
import com.softa.service.system.SysFileService;
import com.softa.vo.QueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yangl on 2017/8/8.
 */
@Service
public class SysFileServiceImpl implements SysFileService {

    @Autowired
    private SysFileRelationMapper sysFileRelationMapper;

    @Autowired
    private SysFileMapper sysFileMapper;

    @Override
    public List<SysFile> selectByCondition(QueryRequest queryRequest) {
        return sysFileRelationMapper.selectByCondition(queryRequest);
    }

    @Override
    public int deleteByPkeyIdAndTableName(QueryRequest queryRequest) {
        return sysFileRelationMapper.deleteByPkeyIdAndTableName(queryRequest);
    }

    @Override
    public SysFile getOne(Long id){
        return sysFileMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysFile selectOneByCondition(QueryRequest queryRequest){
        return sysFileRelationMapper.selectOneByCondition(queryRequest);
    }

    @Override
    public Long getFirstPicId(Long id) {
        QueryRequest queryRequest = new QueryRequest();
        queryRequest.getParams().put("pkeyId", id.toString());
        queryRequest.getParams().put("tableName", Product.class.getSimpleName());
        queryRequest.getParams().put("fileType", FileTypeEnum.IMAGE.toString());
        SysFile sysFile = selectOneByCondition(queryRequest);
        if(sysFile != null){
            return sysFile.getId();
        }else {
            return null;
        }
    }

}
